{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3.12 \u57fa\u672c\u7684\u65e5\u671f\u4e0e\u65f6\u95f4\u8f6c\u6362\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u6267\u884c\u7b80\u5355\u7684\u65f6\u95f4\u8f6c\u6362\uff0c\u6bd4\u5982\u5929\u5230\u79d2\uff0c\u5c0f\u65f6\u5230\u5206\u949f\u7b49\u7684\u8f6c\u6362\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u6267\u884c\u4e0d\u540c\u65f6\u95f4\u5355\u4f4d\u7684\u8f6c\u6362\u548c\u8ba1\u7b97\uff0c\u8bf7\u4f7f\u7528 datetime \u6a21\u5757\u3002\n\u6bd4\u5982\uff0c\u4e3a\u4e86\u8868\u793a\u4e00\u4e2a\u65f6\u95f4\u6bb5\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a timedelta \u5b9e\u4f8b\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from datetime import timedelta\na = timedelta(days=2, hours=6)\nb = timedelta(hours=4.5)\nc = a + b\nc.days"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.seconds"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.seconds / 3600"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.total_seconds() / 3600"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u8868\u793a\u6307\u5b9a\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u5148\u521b\u5efa\u4e00\u4e2a datetime \u5b9e\u4f8b\u7136\u540e\u4f7f\u7528\u6807\u51c6\u7684\u6570\u5b66\u8fd0\u7b97\u6765\u64cd\u4f5c\u5b83\u4eec\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from datetime import datetime\na = datetime(2012, 9, 23)\nprint(a + timedelta(days=10))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "b = datetime(2012, 12, 21)\nd = b - a\nd.days"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "now = datetime.today()\nprint(now)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(now + timedelta(minutes=10))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8ba1\u7b97\u7684\u65f6\u5019\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f datetime \u4f1a\u81ea\u52a8\u5904\u7406\u95f0\u5e74\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = datetime(2012, 3, 1)\nb = datetime(2012, 2, 28)\na - b"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "(a - b).days"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c = datetime(2013, 3, 1)\nd = datetime(2013, 2, 28)\n(c - d).days"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u5927\u591a\u6570\u57fa\u672c\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u95ee\u9898\uff0c datetime \u6a21\u5757\u5df2\u7ecf\u8db3\u591f\u4e86\u3002\n\u5982\u679c\u4f60\u9700\u8981\u6267\u884c\u66f4\u52a0\u590d\u6742\u7684\u65e5\u671f\u64cd\u4f5c\uff0c\u6bd4\u5982\u5904\u7406\u65f6\u533a\uff0c\u6a21\u7cca\u65f6\u95f4\u8303\u56f4\uff0c\u8282\u5047\u65e5\u8ba1\u7b97\u7b49\u7b49\uff0c\n\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 dateutil\u6a21\u5757"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8bb8\u591a\u7c7b\u4f3c\u7684\u65f6\u95f4\u8ba1\u7b97\u53ef\u4ee5\u4f7f\u7528 dateutil.relativedelta() \u51fd\u6570\u4ee3\u66ff\u3002\n\u4f46\u662f\uff0c\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u5c31\u662f\uff0c\u5b83\u4f1a\u5728\u5904\u7406\u6708\u4efd(\u8fd8\u6709\u5b83\u4eec\u7684\u5929\u6570\u5dee\u8ddd)\u7684\u65f6\u5019\u586b\u5145\u95f4\u9699\u3002\u770b\u4f8b\u5b50\u6700\u6e05\u695a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = datetime(2012, 9, 23)\na + timedelta(months=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from dateutil.relativedelta import relativedelta\na + relativedelta(months=+1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a + relativedelta(months=+4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Time between two dates\nb = datetime(2012, 12, 21)\nd = b - a\nd"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "d = relativedelta(b, a)\nd"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "d.months"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "d.days"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}